package com.memory.modules.business.service.sell.impl;

import com.memory.common.data.PageSort;
import com.memory.modules.business.domain.sell.Logistics;
import com.memory.modules.business.repository.sell.LogisticRepository;
import com.memory.modules.business.service.sell.LogisticsService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Optional;

/**
 * @PackageName: com.memory.modules.sell.service.impl
 * @ClassName: LogisticsServiceImpl
 * @Description:
 * @author: zhuguangxun
 * @date 2021/3/10
 */
@Service
@RequiredArgsConstructor
public class LogisticsServiceImpl implements LogisticsService {

    private final LogisticRepository logisticRepository;

    @Override
    public Logistics create(Logistics logistics) {
        logistics.setCreateDate(new Date());
        return logisticRepository.save(logistics);
    }

    @Override
    public Logistics update(Long id, Logistics logistics) {
        logistics.setId(id);
        return findById(id)
                .map(e -> logisticRepository.save(logistics))
                .orElseThrow(() -> new RuntimeException("未获取用户信息"));
    }

    @Override
    public void remove(Long id){
        this.findById(id).ifPresent(e -> logisticRepository.deleteById(id));
    }

    @Override
    public Optional<Logistics> findById(Long id) {
        return logisticRepository.findById(id);
    }

    @Override
    public Page<Logistics> findPage(Example<Logistics> logistics) {
        final PageRequest pageRequest = PageSort.pageRequest();
        return logisticRepository.findAll(logistics, pageRequest);
    }
}
